package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.common.utils.DateUtils;
import com.menggudf.dao.CaseHistoryImgDao;
import com.menggudf.dao.MyCaseHistoryDao;
import com.menggudf.entity.CaseHistoryImg;
import com.menggudf.entity.MyCaseHistory;
import com.menggudf.model.CaseHistoryImgModel;
import com.menggudf.model.MyCaseHistoryModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.MyCaseHistoryService;

@Service
public class MyCaseHistoryServiceImpl implements MyCaseHistoryService{
    
    private static final Log log = LogFactory.getLog(MyCaseHistoryServiceImpl.class);
    
    @Autowired
    MyCaseHistoryDao myCaseHistoryDao;
    
    @Autowired
    CaseHistoryImgDao caseHistoryImgDao;

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel createMyCaseHistory(
            String userCode,
            String roleCode,
            String illNames,
            String symptoms,
            String pictures,
            String examinations,
            String scheme,
            String opinion,
            String medication) {
        try {
            MyCaseHistory my = new MyCaseHistory();
            my.setCode(UUID.randomUUID().toString());
            Date time = new Date();
            my.setCreatedTime(time);
            my.setExaminations(examinations);
            my.setIllNames(illNames);
            my.setMedication(medication);
            my.setName("CH-"+DateUtils.dateFormat1(time));
            my.setOpinion(opinion);
            my.setPictures(pictures);
            my.setRoleCode(roleCode);
            my.setScheme(scheme);
            my.setSymptoms(symptoms);
            my.setUserCode(userCode);
            myCaseHistoryDao.persist(my);
            return ResultModel.success("创建成功", null);
        } catch (Exception e) {
            log.error("createMyCaseHistory fail",e);
        }
        return ResultModel.fail("创建失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getMyCaseHistorys(
            String userCode,
            String roleCode,
            int pageSize,
            int offset) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<MyCaseHistory> list = myCaseHistoryDao.getMyCaseHistorys(userCode,roleCode,pageSize,offset);
        long count = myCaseHistoryDao.getMyCaseHistorysCount(userCode,roleCode);
        List<MyCaseHistoryModel> mlist = new ArrayList<MyCaseHistoryModel>();
        for(MyCaseHistory my : list){
            MyCaseHistoryModel m = new MyCaseHistoryModel();
            m.setId(my.getId());
            m.setCode(my.getCode());
            m.setExaminations(my.getExaminations());
            m.setIllNames(my.getIllNames());
            m.setMedication(my.getMedication());
            m.setName(my.getName());
            m.setOpinion(my.getOpinion());
            m.setPictures(my.getPictures());
            m.setRoleCode(my.getRoleCode());
            m.setScheme(my.getScheme());
            m.setSymptoms(my.getSymptoms());
            m.setUserCode(my.getUserCode());
            m.setCreatedTime(my.getCreatedTime());
            mlist.add(m);
        }
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public MyCaseHistoryModel getMyCaseHistory(int id) {
        MyCaseHistory my = myCaseHistoryDao.findById(id);
        MyCaseHistoryModel m = new MyCaseHistoryModel();
        if(my!=null){
            m.setId(my.getId());
            m.setCode(my.getCode());
            m.setExaminations(my.getExaminations());
            m.setIllNames(my.getIllNames());
            m.setMedication(my.getMedication());
            m.setName(my.getName());
            m.setOpinion(my.getOpinion());
            m.setPictures(my.getPictures());
            m.setRoleCode(my.getRoleCode());
            m.setScheme(my.getScheme());
            m.setSymptoms(my.getSymptoms());
            m.setUserCode(my.getUserCode());
            m.setCreatedTime(my.getCreatedTime());
        }
        return m;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel createCaseHistoryImg(
            String name,
            String userCode,
            String roleCode,
            String imgUrl) {
        try {
            CaseHistoryImg chi = new CaseHistoryImg();
            String code = UUID.randomUUID().toString();
            chi.setCode(code);
            Date time = new Date();
            chi.setCreatedTime(time);
            chi.setImgUrl(imgUrl);
            chi.setName("CH-"+DateUtils.dateFormat1(time));
            chi.setRoleCode(roleCode);
            chi.setUserCode(userCode);
            caseHistoryImgDao.persist(chi);
            return ResultModel.success("创建成功", code);
        } catch (Exception e) {
            log.error("createCaseHistoryImg fail",e);
        }
        return ResultModel.fail("创建失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel addCaseHistoryImg(String code, String imgUrl) {
        CaseHistoryImg ch = caseHistoryImgDao.findByCode(code);
        if(ch!=null){
            try {
                CaseHistoryImg chi = new CaseHistoryImg();
                chi.setCode(code);
                chi.setCreatedTime(new Date());
                chi.setImgUrl(imgUrl);
                chi.setName(ch.getName());
                chi.setRoleCode(ch.getRoleCode());
                chi.setUserCode(ch.getUserCode());
                caseHistoryImgDao.persist(chi);
                return ResultModel.success("添加成功", code);
            } catch (Exception e) {
                log.error("addCaseHistoryImg fail",e);
            }
        }
        return ResultModel.fail("添加失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel delCaseHistoryImg(int id) {
        CaseHistoryImg chi = caseHistoryImgDao.findById(id);
        if(chi!=null){
            try {
                caseHistoryImgDao.remove(chi);
                return ResultModel.success("删除成功", null);
            } catch (Exception e) {
                log.error("delCaseHistoryImg fail",e);
            }
        }
        return ResultModel.fail("删除失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getMyCaseHistoryImgs(
            String userCode,
            String roleCode,
            int pageSize,
            int offset) {
        List<String> codes = caseHistoryImgDao.getMyCaseHistoryImgs(userCode,roleCode,pageSize,offset);
        long count = caseHistoryImgDao.getMyCaseHistoryImgsCount(userCode,roleCode);
        List<CaseHistoryImgModel> mlist = new ArrayList<CaseHistoryImgModel>();
        for(String code : codes){
            CaseHistoryImg ch = caseHistoryImgDao.findByCode(code);
            CaseHistoryImgModel m = new CaseHistoryImgModel();
            m.setCode(code);
            m.setName(ch.getName());
            mlist.add(m);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows", mlist);
        return map;
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public List<CaseHistoryImgModel> getMyCaseHistoryImg(String code) {
        List<CaseHistoryImg> chis = caseHistoryImgDao.findListByCode(code);
        List<CaseHistoryImgModel> mlist = new ArrayList<CaseHistoryImgModel>();
        for(CaseHistoryImg chi : chis){
            CaseHistoryImgModel chim = new CaseHistoryImgModel();
            chim.setCode(code);
            chim.setId(chi.getId());
            chim.setImgUrl(chi.getImgUrl());
            chim.setName(chi.getName());
            mlist.add(chim);
        }
        return mlist;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel delMyCaseHistory(int id) {
        MyCaseHistory my = myCaseHistoryDao.findById(id);
        if(my!=null){
            try {
                myCaseHistoryDao.remove(my);
                return ResultModel.success("删除成功", null);
            } catch (Exception e) {
                log.error("delMyCaseHistory fail",e);
            }
        }
        return ResultModel.fail("删除失败", null);
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel delAllCaseHistoryImgByCode(String code) {
        List<CaseHistoryImg> chis = caseHistoryImgDao.findListByCode(code);
        try {
            for(CaseHistoryImg c : chis){
                caseHistoryImgDao.remove(c); 
            }
            return ResultModel.success("删除成功", null);
        } catch (Exception e) {
            log.error("delAllCaseHistoryImgByCode fail",e);
        }
        return ResultModel.fail("删除失败", null);
    }
}
